<div class="container">
    <h1 align="center"><strong>申请结果</strong></h1>
    <h2 align="center">
        <?php
        include_once "../model/model.php";

        function apply($start, $end) {
            $connect = new CONNECT();
            $connect = $connect->connecttodb();
            $action = $connect->prepare("SELECT * FROM apply_site WHERE activity = ? AND leader = ? AND time_from = ? AND time_to = ?");
            $action->execute(array($_POST["activity"], $_POST["leader"], $start, $end));
            $result = $action->fetchAll();
            if (count($result)) {
                echo "该项目在同一时间段重复申请！";
                return;
            }

            $nameForSite = explode("，", $_POST["name"]);
            if (count($nameForSite) != $_POST["sum"]) {
                echo "申请的位置数和成员名单不符，或者没有使用全角标点！";
                return;
            }

            $request = new REQUEST();
            $num = $request->site_rest($start, $end);
            if ((12 - $num) >= $_POST["sum"]) {
                $connect = new CONNECT();
                $connect = $connect->connecttodb();
                $action = $connect->prepare("SELECT * FROM `blacklist`");
                $action->execute();
                $result = $action->fetchAll();
                $sumofblacklist = count($result);
                for ($j = 0; $j < $_POST["sum"]; $j++) {
                    for ($i = 0; $i < $sumofblacklist; $i++) {
                        if ($result[0][$i] == $nameForSite[$j]) {
                            echo "抱歉，第" . ($j + 1) . "位成员在黑名单中";
                            return;
                        }
                    }
                }
                $connect = new CONNECT();
                $connect = $connect->connecttodb();
                $action = $connect->prepare("INSERT INTO `apply_site`(`activity`, `leader`, `number`, `sum_member`, `sum`, `time_from`, `time_to`, `apply_time`, `detail`, `status`) VALUES (?,?,?,?,?,?,?,?,?,0)");
                $resultofinsert = $action->execute(array($_POST["activity"], $_POST["leader"], $_POST["number"], $_POST["sum_member"], $_POST["sum"], $start, $end, date("Y-n-j H:i:s"), $_POST["detail"]));
                $action1 = $connect->prepare("SELECT `id` FROM `apply_site` WHERE activity = ? AND leader = ? AND time_from = ? AND time_to = ?");
                $action1->execute(array($_POST["activity"], $_POST["leader"], $start, $end));
                $resultofid = $action1->fetchAll();

                if (isset($_POST["others"]) && $_POST["others"] != "") {
                    $action1 = $connect->prepare("INSERT INTO `team_member`(`id`, `member`, `have_site`) VALUES (?,?,-1)");
                    $action1->execute(array($resultofid[0][0], $_POST["others"]));
                }

                $action2 = $connect->prepare("INSERT INTO `team_member`(`id`, `member`, `have_site`) VALUES (?,?,1)");
                for ($i = 0; $i < $_POST["sum"]; $i++) {
                    $resultofinsert = $action2->execute(array($resultofid[0][0], $nameForSite[$i]));
                }

                if ($resultofinsert) {
                    echo "申请成功，等待管理员审核……";
                    return;
                } else {
                    if (!$resultofinsert) {
                        echo "\nPDO::errorInfo():\n";
                        print_r($action2->errorInfo());
                    }
                }
            } else {
                echo "很抱歉，位置不足！";
            }
        }

        if (!isset($_POST["activity"]) || !isset($_POST["leader"]) || !isset($_POST["number"]) ||
            !isset($_POST["sum_member"])) {
            echo "不可能会出现的地方";
        }elseif ($_POST["from_year"] === "" || $_POST["from_month"] === "" || $_POST["from_day"] === "" || 
                 $_POST["to_year"] === "" || $_POST["to_month"] === "" || $_POST["to_day"] === "" ||
                 $_POST["activity"] === "" || $_POST["leader"] === "" || $_POST["number"] === "" ||
                 $_POST["sum_member"] === "" || $_POST["sum"] === ""){
            echo "请填写完整信息！";
        }elseif ($_POST["sum_member"] < $_POST["sum"]) {
            echo "您不能申请大于项目总人数的位置数！";
        }elseif (!is_numeric($_POST["from_year"]) || !is_numeric($_POST["from_month"]) || 
                 !is_numeric($_POST["from_day"]) || !is_numeric($_POST["to_year"]) || 
                 !is_numeric($_POST["to_month"]) || !is_numeric($_POST["to_day"])) {
            echo "时间格式有误！not number";
        }else {
            $start = $_POST["from_year"] . "-" . $_POST["from_month"] . "-" . $_POST["from_day"];
            $end = $_POST["to_year"] . "-" . $_POST["to_month"] . "-" . $_POST["to_day"];
            $now = date("Y-n-j");
            if (!checkdate($_POST["from_month"], $_POST["from_day"], $_POST["from_year"]) ||
                !checkdate($_POST["to_month"], $_POST["to_day"], $_POST["to_year"])) {
                echo "时间格式有误！illeagal";
            }elseif (strtotime($start) < strtotime($now)) {
                echo "申请时间有误！";
            }elseif (strtotime($start) > strtotime($end)) {
                echo "起始时间不能大于终止时间！";
            }else{
                apply($start, $end);
            }
        }
    //     if (isset($_POST["from_year"]) && $_POST["from_year"] != "" && $_POST["from_month"] != "" && $_POST["from_day"] != "" && $_POST["to_year"] != "" && $_POST["to_month"] != "" && $_POST["to_day"] != "" && $_POST["activity"] != "" && $_POST["leader"] != "" && $_POST["number"] != "" && $_POST["sum_member"] != "" && $_POST["sum"] != "") {
    //         if ($_POST["sum_member"] >= $_POST["sum"]) {
				// $pattern = "/0([1-9])/";
				// $replacement = "$1";
				// $_POST["from_month"] = preg_replace($pattern, $replacement, $_POST["from_month"]);
				// $_POST["to_month"] = preg_replace($pattern, $replacement, $_POST["to_month"]);
    //             if (is_numeric($_POST["from_year"]) && is_numeric($_POST["from_month"]) && is_numeric($_POST["from_day"]) && is_numeric($_POST["to_year"]) && is_numeric($_POST["to_month"]) && is_numeric($_POST["to_day"])) {
    //                 $start = $_POST["from_year"] . "-" . $_POST["from_month"] . "-" . $_POST["from_day"];
    //                 $end = $_POST["to_year"] . "-" . $_POST["to_month"] . "-" . $_POST["to_day"];
    //                 if (checkdate($_POST["from_month"], $_POST["from_day"], $_POST["from_year"]) && checkdate($_POST["to_month"], $_POST["to_day"], $_POST["to_year"])) {
    //                     $now = date("Y-n-j");
    //                     if (strtotime($start) >= strtotime($now)) {
    //                         if (strtotime($start) <= strtotime($end)) {
    //                             apply($start, $end);
    //                         } else {
    //                             echo "起始时间不能大于终止时间！";
    //                         }
    //                     } else {
    //                         echo "申请时间有误！";
    //                     }
    //                 } else {
    //                     echo "时间格式有误！illeagal";
    //                 }
    //             } else {
    //                 echo "时间格式有误！not number";
    //             }
    //         } else {
    //             echo "您不能申请大于项目总人数的位置数！";
    //         }
    //     } else {
    //         echo "请填写完整信息！";
    //     }
        ?>
        <a href="../user/form1.php">返回</a>
    </h2>
</div>
